import sys

from typing import List

from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models
from alibabacloud_tea_util.client import Client as UtilClient


dingtalk_access_token = ""

class DingtalkCient:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> dingtalkoauth2_1_0Client:
        """
        使用 Token 初始化账号Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config()
        config.protocol = 'https'
        config.region_id = 'central'
        return dingtalkoauth2_1_0Client(config)

    @staticmethod
    def refresh_access_token() -> None:
        refresh_xm_token()


# 霞铭access_token
def refresh_xm_token():
    client = DingtalkCient.create_client()
    get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest(
        app_key='dingkodcuposp9elh5bl',
        app_secret='8_LfSvEMdC45rLwTrNKvJzDgUnnQqS64rs6I2IjXc0jtb7OVJI4eOh_b3SDQf9Oo'
    )
    try:
        res = client.get_access_token(get_access_token_request)
        global dingtalk_access_token
        dingtalk_access_token = res.body.access_token
    except Exception as err:
        if not UtilClient.empty(err.code) and not UtilClient.empty(err.message):
            # err 中含有 code 和 message 属性，可帮助开发定位问题
            pass


